﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Kopytko.BoardGame
{
    /// <summary>
    /// Interaction logic for Board.xaml
    /// </summary>
    public partial class Board : UserControl
    {
        const byte Size = 8;

        public BoardPosition Selected { get; set; }
        public List<BoardBox> Boxes { get; set; }

        public BoardLogic Logic { get; protected set; }
        
        public Board(BoardLogic logic)
        {
            InitializeComponent();
            Boxes = new List<BoardBox>();
            Logic = logic;
            Logic.PlayerChanged += new EventHandler(Logic_PlayerChanged);
            Logic.Reload += new EventHandler(Logic_Reload);

            Logic.Init();


            for (byte i = 0; i < Size; i++)
                for (byte j = 0; j < Size; j++)
                {
                    BoardBox bb = new BoardBox(j, i);
                    Boxes.Add(bb);
                    bb.Selected += new EventHandler(bb_Selected);
                    grid_main.Children.Add(bb);

                    Grid.SetRow(bb, i);
                    Grid.SetColumn(bb, j);
                }

            setBoard();
        }

        void setBoard()
        {
            foreach (var p in Logic.Pawns)
            {
                grid_main.Children.Add(p);

                Grid.SetRow(p, p.Y);
                Grid.SetColumn(p, p.X);
            }
        }

        void Logic_Reload(object sender, EventArgs e)
        {
            List<Pawn> toRemove = new List<Pawn>();
            
            foreach (var c in grid_main.Children)
            {
                if (c is Pawn)
                {
                    toRemove.Add(c as Pawn);
                }
            }

            foreach (var r in toRemove)
            {
                grid_main.Children.Remove(r);
            }

            setBoard();
        }

        void Logic_PlayerChanged(object sender, EventArgs e)
        {
            
        }

        private BoardBox getBoardBox(BoardPosition boardPosition)
        {
            var bb = (from c in Boxes
                      where (c as BoardBox).X == boardPosition.X && (c as BoardBox).Y == boardPosition.Y
                      select (BoardBox) c).Single();
            return bb;
        }


        private void markPossibleMovements(BoardPosition currentPostion)
        {
            var possibleMovements = Logic.GetAvailableMovements(currentPostion);

            foreach (var p in possibleMovements)
            {
                getBoardBox(p).IsHighlighted = true;
            }
        }

        private void dehighlightAll()
        {
            var higthlighted = Boxes.Where(b => b.IsHighlighted);

            foreach (var b in higthlighted)
            {
                b.IsHighlighted = false;
            }
        }

        void bb_Selected(object sender, EventArgs e)
        {
            BoardBox bb = sender as BoardBox;



            if (Selected != null)
            {
                var oldBb = getBoardBox(Selected);
                oldBb.IsSelected = false;

                var ok = Logic.GetAvailableMovements(Selected).Any(b=>b.X == bb.X && b.Y == bb.Y);

                if (ok)
                {
                    var result = Logic.Move(Selected, new BoardPosition(bb.X, bb.Y));

                    if (result.BeatedPawns != null)
                    {
                        foreach (var b in result.BeatedPawns)
                        {
                            grid_main.Children.Remove(b);
                        }
                    }

                    if (result.CurrentPlayer!=null)
                        tb_gracz.Text = result.CurrentPlayer.Name;
                }
                else
                {
                    
                }

                dehighlightAll();
                Selected = null;
            }
            else
            {
                var bp = new BoardPosition(bb.X, bb.Y);
                if (Logic.IsMoveAllowed(bp))
                {
                    bb.IsSelected = true;
                    Selected = bp;
                    markPossibleMovements(Selected);
                }
            }
        }
    }
}
